import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:dailypayagent/model/sales.dart';
import 'package:dailypayagent/provider/provider_widget.dart';
import 'package:dailypayagent/provider/view_state_widget.dart';
import 'package:dailypayagent/ui/helper/refresh_helper.dart';
import 'package:dailypayagent/ui/widget/sales_list_item.dart';
import 'package:dailypayagent/view_model/sale_model.dart';
import 'package:oktoast/oktoast.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class SalesManager extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return WechatArticleList();
  }
}

class WechatArticleList extends StatefulWidget {
  WechatArticleList();

  @override
  _WechatArticleListState createState() => _WechatArticleListState();
}

class _WechatArticleListState extends State<WechatArticleList>
    with AutomaticKeepAliveClientMixin {
  @override
  bool get wantKeepAlive => true;

  List<DropdownMenuItem> getItemList() {
    var dropStringList = List<DropdownMenuItem>();
    dropStringList.add(DropdownMenuItem(
      value: '全部',
      child: Text('全部'),
    ));
    dropStringList.add(DropdownMenuItem(
      value: '使用中',
      child: Text('使用中'),
    ));
    dropStringList.add(DropdownMenuItem(
      value: '冻结',
      child: Text('冻结'),
    ));
    return dropStringList;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    String _selectedSort = '全部';

    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('业务员列表'),
          actions: <Widget>[
            FlatButton(
              textColor: Colors.white,
              onPressed: () {},
              child: Text("新增"),
              shape: CircleBorder(side: BorderSide(color: Colors.transparent)),
            )
          ],
        ),
        body: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    DropdownButton(
                      value: _selectedSort,
                      underline: Container(
                        height: 0,
                      ),
                      items: getItemList(),
                      onChanged: (T) {
                        setState(() {
                          showToast(T);
                        });
                      },
                    ),
                    SizedBox(
                      width: 1,
                      height: 20,
                      child: DecoratedBox(
                        decoration: BoxDecoration(color: Colors.grey[300]),
                      ),
                    ),
                    Row(
                      children: <Widget>[Text('搜索'), Icon(Icons.search)],
                    )
                  ],
                ),
              ),
              SizedBox(
                width: 360,
                height: 1,
                child: DecoratedBox(
                  decoration: BoxDecoration(color: Colors.grey[300]),
                ),
              ),
              Expanded(
                child: ProviderWidget<SalesListModel>(
                  model: SalesListModel(),
                  onModelReady: (model) => model.initData(),
                  builder: (context, model, child) {
                    if (model.isBusy) {
                      return ViewStateBusyWidget();
                    } else if (model.isError) {
                      return ViewStateErrorWidget(
                          error: model.viewStateError,
                          onPressed: model.initData);
                    } else if (model.isEmpty) {
                      return ViewStateEmptyWidget(onPressed: model.initData);
                    }
                    return SmartRefresher(
                        controller: model.refreshController,
                        header: WaterDropHeader(),
                        footer: RefresherFooter(),
                        onRefresh: model.refresh,
                        onLoading: model.loadMore,
                        enablePullUp: true,
                        child: ListView.builder(
                            itemCount: model.list.length,
                            itemBuilder: (context, index) {
                              Sales item = model.list[index];
                              return SalesItemWidget(item);
                            }));
                  },
                ),
              )
            ]));
  }
}
